package com.gexiaobao.pigeon.ui.pilot.activity

import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.Constant
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.CacheUtil
import com.gexiaobao.pigeon.app.util.KvUtils
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.ActivityReleaseMissionBinding
import com.gexiaobao.pigeon.ui.activity.LoginActivity
import com.gexiaobao.pigeon.ui.adapter.MyPagerAdapter
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.ui.pilot.fragment.FragmentComplete
import com.gexiaobao.pigeon.ui.pilot.fragment.FragmentUnDone
import com.gexiaobao.pigeon.viewmodel.ReleaseMissionViewModel
import me.hgj.jetpackmvvm.ext.lifecycle.KtxActivityManger
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 11:24
 * @Describe    : 放飞任务
 */
class ReleaseMissionActivity :
    BaseActivity<ReleaseMissionViewModel, ActivityReleaseMissionBinding>() {

    private var orgId = ""
    var exitTime = 0L

    override fun initView(savedInstanceState: Bundle?) {
        CacheUtil.setFirst(false)
        mDatabind.toolbar.init("放飞任务")
        orgId = KvUtils.decodeInt(Constant.ORGANIZATION_ID).toString()
        initViewPager(orgId)

        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                if (System.currentTimeMillis() - exitTime > 2000) {
                    RxToast.showToast("再按一次退出程序")
                    exitTime = System.currentTimeMillis()
                } else {
                    finish()
                }
            }
        })
    }

    private fun initViewPager(orgId: String) {
        val tabTitles = arrayListOf("未完成", "已完成")
        val fragments = ArrayList<Fragment>()
        fragments.add(FragmentUnDone.newInstance(orgId))
        fragments.add(FragmentComplete.newInstance(orgId))
        mDatabind.viewPager.adapter =
            MyPagerAdapter(supportFragmentManager, this, fragments, tabTitles)
        mDatabind.viewPager.offscreenPageLimit = 2
        mDatabind.tabLayout.setupWithViewPager(mDatabind.viewPager)

    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvLogout) {
            when (it) {
                mDatabind.tvLogout -> {
                    showDialog()
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.logOutResult.observe(this) {
            dismissLoading()
            if (it.isSuccess) {
                CacheUtil.setIsLogin(false)
                KtxActivityManger.finishAllActivity()
                RxToast.showToast("已退出登录")
                startActivity<LoginActivity>()
                //带点渐变动画
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }

    private fun showDialog() {
        val dialog = RxDialogDefault(this)
        dialog.setTitle("确定退出吗？")
        dialog.setSureListener {
            dialog.dismiss()
            showLoading("退出中...")
            mViewModel.loginOut()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }
}